using B.T.BasicData.Infraeturcture;
using MES_workbench.Api.Filters;
using MES_workbench.Api.Implementationlayer.IServices;
using MES_workbench.Api.Implementationlayer.Services;
using MES_workbench.Infraetructure;
using Microsoft.EntityFrameworkCore;
using NLog.Web;

var builder = WebApplication.CreateBuilder(args);
// Add services to the container.

// 全局异常过滤器注册
builder.Services.AddControllers(options =>
{
    options.Filters.Add<GlobalExceptionFilter>();
});
// 缓存（用于热点查询降压）
builder.Services.AddMemoryCache();
// HttpClient 工厂（用于外部 API 调用，如天气）
builder.Services.AddHttpClient();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
//中文注释
builder.Services.AddSwaggerGen(x =>
{
    var file = Path.Combine(AppContext.BaseDirectory + "MES_workbench.Api.xml");
    x.IncludeXmlComments(file, true);
});


//配置EF Core
builder.Services.AddDbContext<EFDbContext>(x =>
{
    x.UseMySql(builder.Configuration.GetConnectionString("mysql"), ServerVersion.AutoDetect(builder.Configuration.GetConnectionString("mysql")));
});

//注入
builder.Services.AddScoped(typeof(IBaseRepository<>), typeof(BaseRepository<>));
builder.Services.AddScoped<IWorkbenchServices,WorkbenchServices>();
// 过滤器依赖注入（用于构造函数注入 ILogger/IWebHostEnvironment 等）
builder.Services.AddScoped<GlobalExceptionFilter>();


//日志
builder.Host.UseNLog();




var app = builder.Build();
//跨域
app.UseCors(x => x.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());

// Configure the HTTP request pipeline.
//if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseAuthorization();

app.MapControllers();

app.Run();
